package work.managercenter;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import utils.MyUtils;

public class VRPPost {

	private static final String fileURL = "/home/gmarcone/Carpeta sin título/189004.xml";

	// Local complex id 3101234567
//	private static final String url = "http://localhost:8000/services/partner/v1/postVerifiedResidentFeed";
//	private static final String partnerToken = "15374060-8768-4ed1-b9fe-07f4974ada62";

	// Staging 999999999912345 - 484638695019610
	private static final String url = "http://aptrest1.internetbrands.com:8080/services/partner/v1/postVerifiedResidentFeed";
	private static final String partnerToken = "ad67e352-54b7-445d-9894-895c769974a7";

	// Prod 9199332346275143307
//	private static final String url = "http://aptrest1.internetbrands.com:8080/services/partner/v1/postVerifiedResidentFeed";
//	private static final String partnerToken = "2a2a8a33-14a4-4305-bf07-5d8641776dc6";

	public static void main(String[] args) {
		try {
			String xml = Base64.encodeBase64String(MyUtils.readFileToString(new File(fileURL)).getBytes(Charset.defaultCharset()));

			DefaultHttpClient httpclient = new DefaultHttpClient();

			HttpPost httpost = new HttpPost(url);

			List<NameValuePair> postParams = new ArrayList<NameValuePair>();
			postParams.add(new BasicNameValuePair("base64EncodedXML", xml));
			postParams.add(new BasicNameValuePair("token", partnerToken));

			httpost.setEntity(new UrlEncodedFormEntity(postParams));
			HttpResponse response = httpclient.execute(httpost);

			String finalReturn = inputStreamToString(response.getEntity().getContent());

			httpclient.getConnectionManager().shutdown();

			System.out.println(finalReturn);

		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	private static String inputStreamToString(InputStream is) throws IOException {
		return MyUtils.toString(is, null);
	}

}
